<?php

namespace app\api\service;

use think\Request;

class Image extends BaseService
{
    //上传文件 返回不压缩地址
    public function upload($path_){

        $file = Request::instance()->file('file');

        if ($file){
            $path = ROOT_PATH . 'public' . DS . 'upload' . DS . $path_;
            $info = $file->move($path);

            if ($info && $info->getPathname()) {
                $imagePath = $info->getSaveName();
                $image = \think\Image::open($path.'/'.$imagePath);
                $date_path_real = $path.'/'.date('Ymd');
                $date_path = date('Ymd');
                if(!file_exists($date_path_real)){
                    mkdir($date_path,0777,true);
                }

                $fileName = 'thumb_'.$info->getFilename();
                $thumb_real_path = $date_path_real.'/'.$fileName;
                $thumb_path = $date_path.'/'.$fileName;
                $image->thumb(300, 300, \think\Image::THUMB_SCALING)->save($thumb_real_path);
                return $thumb_path;
            }
        }
        return throwError('更新失败');
    }

}